package com.example.jh352160.surfingtv.Index.childpage;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.jh352160.surfingtv.Index.customView.adapter.AllCategoryAdapter;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.AllCategoryItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.AllCategoryService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/28
 */
public class AllCategory extends AppCompatActivity {

    private static final int NETWORK_OK = 0;
    Toolbar toolbar;
    RecyclerView allCategory;
    List<String> titleList;
    List<String> imageUrlList;
    List<Integer> programIdList;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NETWORK_OK:
                    init();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index_all_category);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AllCategory.this.finish();
                Toast.makeText(AllCategory.this, "asdasdasdsa", Toast.LENGTH_LONG).show();
            }
        });
        allCategory = (RecyclerView) findViewById(R.id.allCategory);
//        CommodityRecyclerAdapter adapter=new CommodityRecyclerAdapter();
        allCategory.setLayoutManager(new GridLayoutManager(this, 2));
//        allCategory.setAdapter(adapter);

        int adType = getIntent().getIntExtra("category", 0);
        Retrofit retrofit = CommonRetrofit.getRetrofit();
        AllCategoryService allCategoryService = retrofit.create(AllCategoryService.class);
        Call<AllCategoryItem> call = allCategoryService.getItem(adType);
        call.enqueue(new Callback<AllCategoryItem>() {
            @Override
            public void onResponse(Call<AllCategoryItem> call, Response<AllCategoryItem> response) {
                List<AllCategoryItem.DataBean.ItemsBean> items = response.body().getData().getItems();
                titleList = new ArrayList<>();
                imageUrlList = new ArrayList<>();
                programIdList = new ArrayList<>();
                for (int i = 0; i < items.size(); i++) {
                    titleList.add(items.get(i).getTitle());
                    imageUrlList.add(items.get(i).getImageUrl());
                    programIdList.add(items.get(i).getProgramId());
                }
                Message message = new Message();
                message.what = NETWORK_OK;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<AllCategoryItem> call, Throwable t) {
                Log.e("Retrofit Error", t.toString());
            }
        });
    }

    private void init() {
        AllCategoryAdapter adapter = new AllCategoryAdapter(this, titleList, imageUrlList, programIdList);
        allCategory.setAdapter(adapter);
    }
}
